package com.mercator.client.graph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class PhenomeMap extends Graph<GenClique, PhenomeNode> {

	ArrayList<HashSet<PhenomeNode>> pset;
	Set<PhenomeNode> roots;

	int max;

	public PhenomeMap() {
		max = 0;
		pset = new ArrayList<HashSet<PhenomeNode>>();
		roots = new HashSet<PhenomeNode>();
	}

	public void add(PhenomeNode n) {
		super.add(n);
		int degree = n.size();
		if (degree >= max) {
			if ((degree == 1 && max == 0)) {
				pset.add(new HashSet<PhenomeNode>());
				max = degree;
			} else {
				roots.add(n);
				for (int i = 0; i < degree - max; i++)
					pset.add(new HashSet<PhenomeNode>());
				max = degree;
				connectAll(n, pset.get(max - 1));
			}
			
			
		} else if (degree < max) {
			connectAll(n, pset.get(degree));
			if(isRootNode(n))
				roots.add(n);
			if (degree != 1)
				connectAll(n, pset.get(degree - 1 - 1));
		}
		
		pset.get(degree - 1).add(n);
	}

	private static void connectAll(PhenomeNode p, Collection<PhenomeNode> pc) {
		PhenomeNode n1 = p;
		PhenomeNode n2;
		for (PhenomeNode v : pc) {
			n2 = v;
			n1 = (v.size() > p.size()) ? v : p;
			n2 = (n1 != n2) ? v : p;
			if (n1.containsAll(n2) && n1 != null && n2 != null){
				n1.connect(n2);
				n2.connect(n1);
				}
		}
	}

	@Override
	public void addAll(Collection<PhenomeNode> col) {
		super.addAll(col);

	}
	
	public boolean isRootNode(PhenomeNode pn){
		boolean isRoot = true;
		for(PhenomeNode n : pn)
			isRoot &= (pn.size() > n.size());
		return isRoot;
	}

	public Set<PhenomeNode> getRoot() {
		return roots;
	}

	public void connect(PhenomeNode v1, PhenomeNode v2) {
		v2.connect(v1);
	}

	public class PhenomeMapIterator implements Iterator<PhenomeNode> {
		private int current_index;
		private int inner_index;

		public PhenomeMapIterator() {
			current_index = max;
			inner_index = 0;
		}

		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public PhenomeNode next() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub

		}

	}
}
